//实例
var stompClient = null;


//实例化websocket
    var socket = new SockJS('/endpointSang');
    stompClient = Stomp.over(socket); //创建websocket客户端
    stompClient.connect({}, function (frame) {

        //用来接收消息
        stompClient.subscribe('/topic/getResponse', function (response) {
            alert(response);
            //接收的信息
            var message = JSON.parse(response.body).responseMessage;
            createClient(message);
        })
    });


/*function up_say() {
    $('.write_list').remove();
    var text = $('.write_box textarea').val(),
        str = '<div class="question">';
        str += '<div class="heard_img right"><img src="/chat/images/2.jpg"></div>';
        str += '<div class="question_text clear"><p>' + text + '</p><i></i>';
        str += '</div></div>';

    if (text == '') {
        alert('不能发送空内容');
        $('.write_box textarea').focus();
    } else {
        $('.speak_box').append(str);
        $('.write_box textarea').val('');
        $('.write_box textarea').focus();
        autoWidth();
        for_bottom();
        setTimeout(function () {
            var ans = '<div class="answer"><div class="heard_img left"><img src="/chat/images/1.jpg"></div>';
            ans += '<div class="answer_text"><p>您发送的文字是：' + text + '</p><i></i>';
            ans += '</div></div>';
            $('.speak_box').append(ans);
            for_bottom();
        }, 1000);
    }
}*/

var wen = document.getElementById('wenwen');

function _touch_start(event) {
    event.preventDefault();
    $('.wenwen_text').css('background', '#c1c1c1');
    $('.wenwen_text span').css('color', '#fff');
    $('.saying').show();
}

/* wen.addEventListener("touchstart", _touch_start, false);
 wen.addEventListener("touchend", _touch_end, false);*/

function for_bottom() {
    var speak_height = $('.speak_box').height();
    $('.speak_box,.speak_window').animate({scrollTop: speak_height}, 500);
}

function autoWidth() {
    $('.question_text').css('max-width', $('.question').width() - 60);
}

//发送消息
function sendMessage() {
    var text = $('.write_box textarea').val();
    $('.write_list').remove();
    var text = $('.write_box textarea').val(),
        str = '<div class="question">';
        str += '<div class="heard_img right"><img src="/chat/images/2.jpg"></div>';
        str += '<div class="question_text clear"><p>' + text + '</p><i></i>';
        str += '</div></div>';

    if (text == '') {
        alert('不能发送空内容');
        $('.write_box textarea').focus();
    } else {
        $('.speak_box').append(str);
        $('.write_box textarea').val('');
        $('.write_box textarea').focus();
        autoWidth();
        for_bottom();
        stompClient.send("/welcome", {}, JSON.stringify({'name': text}));
    }
}

//获得服务器消息后 创建界面
function createClient(message){
    var ans = '<div class="answer"><div class="heard_img left"><img src="/chat/images/1.jpg"></div>';
    ans += '<div class="answer_text"><p>您发送的文字是：' + message + '</p><i></i>';
    ans += '</div></div>';
    $('.speak_box').append(ans);
    for_bottom();
}


